<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./test.js"></script>
    <style>
        div{
            position: absolute;
            left: 0px;
            width: 100px;
            height: 100px;
            background: pink;
            border-radius: 50%;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <button>向右运动</button>
    <button>向左运动</button>
    <div></div>
    <script>
        var odiv = document.getElementsByTagName('div')
        var btn = document.getElementsByTagName('button')
        var time = null
        var time1 = null
        var centers = 0
        function fn(){
            var numLeft = null
            if(centers == 1) {
                numLeft = parseInt(getStyle((odiv[0]),'left')) + 10
                if(numLeft>500) {
                    numLeft=500
                    clearTimeout(time)
                    return numLeft
                }
           }else if (centers == 2) {
                numLeft = parseInt(getStyle((odiv[0]),'left')) - 10
                if(numLeft<0) {
                    numLeft=0
                    clearTimeout(time)
                    return numLeft
                }
           }
           odiv[0].style.left=numLeft+'px'
        }
        btn[1].onclick = function(){
            centers = 1
            clearTimeout(time)
            time=setInterval(fn,50)
        }
        btn[0].onclick = function(){
            centers = 2
            clearTimeout(time)
            time=setInterval(fn,50)
            
        }
        
</script>
</body>
</html>